<?php
/*
	[BuyPg] (C) 2010-2011 Buypg
	$Id: SearchAction.class.php 2011-6-8 10:24:30 wufujun $
*/
if (!defined('THINK_PATH')) {
	exit('Access Denied');
}

class SearchAction extends BpAction{
	public function index(){
		global $_SGLOBAL;
		$keyword = empty ($_GET['keyword']) ? '' : $_GET['keyword'];
		import("@.ORG.Input");
		$keyword = Input::deleteHtmlTags($keyword);
		
		$arrSite = searchWebsite($keyword , 10);
		foreach ($arrSite as $key => $value){
			$arrSite[$key]['f1'] = formatFen($value['f1']);
			$arrSite[$key]['f2'] = formatFen($value['f2']);
			$arrSite[$key]['f3'] = formatFen($value['f3']);
			$arrSite[$key]['f4'] = formatFen($value['f4']);
			$arrSite[$key]['f5'] = formatFen($value['f5']);
			$arrSite[$key]['f6'] = formatFen($value['f6']);
			$arrSite[$key]['f7'] = formatFen($value['f7']);
		}
		
		$arrNews = searchNews($keyword , 10 , "news_id DESC");
		foreach ($arrNews as $key => $value){
			$arrNews[$key]['news_content'] = trim(msubstr(Input::deleteHtmlTags(Input::safeHtml($value['news_content'])), 0, 300, 'utf-8', FALSE));
		}
		
		$arrTousu = searchTousu($keyword , 10 , "tousu_id DESC");
		foreach ($arrTousu as $key => $value){
			$arrTousu[$key]['tousu_content'] = trim(msubstr(Input::deleteHtmlTags(Input::safeHtml($value['tousu_content'])), 0, 300, 'utf-8', FALSE));
		}
		
		
		//模板输出
		$this->assign('keyword' , $keyword);
		$this->assign('arrSite' , $arrSite);
		$this->assign('arrNews' , $arrNews);
		$this->assign('arrTousu' , $arrTousu);
		$this->display();
		
	}
	public function search(){
		global $_SGLOBAL;
		$type = isset ($_GET['type']) ? $_GET['type'] : 'website';
		$keyword = empty ($_GET['keyword']) ? '' : $_GET['keyword'];
		import("@.ORG.Input");
		//$keyword = iconv("GBK", "UTF-8", $keyword); //不知道为什么，传过来的是GBK的，在本机没有问题，服务器上就是乱码，已经检查过文件编码，都没有问题
		$keyword = Input::deleteHtmlTags($keyword);
		
		$arrkey = explode(" ",$keyword);
		switch ($type){
			case 'youhui':
				$DbYouhui = M('Youhui');
				foreach($arrkey as $key => $value){
					if($key < 1){
						$where['_string'] = "(title like '%$value%')OR(message like '%$value%')";
					}else{
						$where['_string'] .= "OR(title like '%$value%')OR(message like '%$value%')";
					}
				}
				$where['status'] = '1';
				import("@.ORG.Page"); //导入分页类
				$count = $DbYouhui->where($where)->count();    //计算总数
				$p = new Page ( $count, 20 );
				$arrYouhui = $DbYouhui->where($where)->limit($p->firstRow.','.$p->listRows)->order("stime ASC")->select();
				
				foreach ($arrYouhui as $k => $v){
					$arrYouhui[$k]['message'] = trim(msubstr(Input::deleteHtmlTags(Input::safeHtml($v['message'])), 0, 300, 'utf-8', FALSE));
				}
				
				$p->setConfig('header','篇记录');
				$p->setConfig('prev','<');
				$p->setConfig('next','>');
				$p->setConfig('first','<<');
				$p->setConfig('last','>>');

				$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
				$page = $p->show(0);
				$this->assign('arrYouhui' , $arrYouhui);
				break;
			case 'news':
				$DbNews = M("News");
				foreach($arrkey as $key => $value){
					if($key < 1){
						$where['_string'] = "(news_title like '%$value%')OR(news_content like '%$value%')";
					}else{
						$where['_string'] .= "OR(news_title like '%$value%')OR(news_content like '%$value%')";
					}
				}
				import("@.ORG.Page"); //导入分页类
				$count = $DbNews->where($where)->count();    //计算总数
				$p = new Page ( $count, 20 );
				$arrNews = $DbNews->where($where)->limit($p->firstRow.','.$p->listRows)->order("news_id DESC")->select();
				
				foreach ($arrNews as $k => $v){
					$arrNews[$k]['news_content'] = trim(msubstr(Input::deleteHtmlTags(Input::safeHtml($v['news_content'])), 0, 300, 'utf-8', FALSE));
				}
				
				$p->setConfig('header','篇记录');
				$p->setConfig('prev','<');
				$p->setConfig('next','>');
				$p->setConfig('first','<<');
				$p->setConfig('last','>>');

				$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
				$page = $p->show(0);
				$this->assign('arrNews' , $arrNews);
				break;
			case 'tousu':
				$DbTousu = M("NbTousu");
				foreach($arrkey as $key => $value){
					if($key < 1){
						$where['_string'] = "(tousu_title like '%$value%')OR(tousu_content like '%$value%')";
					}else{
						$where['_string'] .= "OR(tousu_title like '%$value%')OR(tousu_content like '%$value%')";
					}
				}
				import("@.ORG.Page"); //导入分页类
				$count = $DbTousu->where($where)->count();    //计算总数
				$p = new Page ( $count, 20 );
				$arrTousu = $DbTousu->where($where)->limit($p->firstRow.','.$p->listRows)->order("tousu_id DESC")->select();
				
				foreach ($arrTousu as $k => $v){
					$arrTousu[$k]['tousu_content'] = trim(msubstr(Input::deleteHtmlTags(Input::safeHtml($v['tousu_content'])), 0, 300, 'utf-8', FALSE));
				}
				
				$p->setConfig('header','篇记录');
				$p->setConfig('prev','<');
				$p->setConfig('next','>');
				$p->setConfig('first','<<');
				$p->setConfig('last','>>');

				$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
				$page = $p->show(0);
				$this->assign('arrTousu' , $arrTousu);
				break;
			case 'website':
			default :
				$DbSite = M("NbSite");
				$where['flag'] = array('eq' , 1);
				foreach($arrkey as $key => $value){
					if($key < 1){
						$where['_string'] = "(site_name like '%$value%')OR(site_desc like '%$value%')OR(site_link like '%$value%')";
					}else{
						$where['_string'] .= "OR(site_name like '%$value%')OR(site_desc like '%$value%')OR(site_link like '%$value%')";
					}
				}
				import("@.ORG.Page"); //导入分页类
				$count = $DbSite->where($where)->count();    //计算总数
				$p = new Page ( $count, 20 );
				$arrSite = $DbSite->where($where)->limit($p->firstRow.','.$p->listRows)->select();
				
				foreach ($arrSite as $key => $value){
					$arrSite[$key]['f1'] = formatFen($value['f1']);
					$arrSite[$key]['f2'] = formatFen($value['f2']);
					$arrSite[$key]['f3'] = formatFen($value['f3']);
					$arrSite[$key]['f4'] = formatFen($value['f4']);
					$arrSite[$key]['f5'] = formatFen($value['f5']);
					$arrSite[$key]['f6'] = formatFen($value['f6']);
					$arrSite[$key]['f7'] = formatFen($value['f7']);
				}

				$p->setConfig('header','篇记录');
				$p->setConfig('prev','<');
				$p->setConfig('next','>');
				$p->setConfig('first','<<');
				$p->setConfig('last','>>');

				$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
				$page = $p->show(0);
				
				$this->assign('arrSite' , $arrSite);
				break;
		}

		//当前位置
		$position = " &gt; 搜索 &gt; 关键词：".$keyword;

		//板模输出
		$this->assign('page' , $page);
		$this->assign('keyword' , $keyword);
		$this->assign('position' , $position);
		$this->display('buypg@Search:search_'.$type);
		
	}
	public function ajaxsearch(){
		global $_SGLOBAL;
		C ( 'SHOW_PAGE_TRACE', false ); //关闭调试信息
		$keyword = empty ($_POST['keyword']) ? '' : $_POST['keyword'];
		$map['flag'] = array('eq' , 1);
		if($keyword){
			$map['_string'] = "site_name like '%$keyword%' or site_link like '%$keyword%'";
		}
		$DbSite = M("NbSite");
		$arrSite = $DbSite->where($map)->order("f7 DESC")->limit(15)->select();
		//格式化分数
		foreach ($arrSite as $key => $value){
			$arrSite[$key]['f7'] = formatFen($value['f7']);
		}
		//模板输出
		$this->assign('arrSite' , $arrSite);
		$this->display();
	}
	
	public function ajaxsearchshow(){
		global $_SGLOBAL;
		C ( 'SHOW_PAGE_TRACE', false ); //关闭调试信息
		$siteid = isset($_POST['siteid']) ? intval($_POST['siteid']) : 0;
		$DbSite = D("NbSite");
		$arrSite = $DbSite->getSite("site_id = $siteid and flag = 1");
		if(!$arrSite){
			$this->error('您查看的信息不存在或已经删除');
		}
		
		//获取alexa
		$arrAlexa = array();
		$arrAlexa = GetAlexa(urldecode($arrSite['site_link']));
		
		//获取回头率
		$DbSiteHt = M("SiteHt");
		$DbNbHt = M("NbHt");
		
		$arrSiteHt = $DbSiteHt->where("site_id = $siteid")->find();
		if($arrSiteHt){
			$Htl = round($arrSiteHt['ht1'] / ($arrSiteHt['ht1'] + $arrSiteHt['ht2']),2) * 100; 
		}else{
			$Ht1 = $DbNbHt->where("site_id = $siteid and ht = 1")->count();
			$HtSum = $DbNbHt->where("site_id = $siteid")->count();
			$Htl = round(($Ht1 / $HtSum) , 2) * 100 ;
		}
		
		//模板输出
		$this->assign('arrAlexa' , $arrAlexa);
		$this->assign('Htl' , $Htl);
		$this->assign('arrSite' , $arrSite);
		$this->display();
	}
}
?>
